package com.ruoyi.project;

import com.ruoyi.project.pay.RSA;
import com.ruoyi.project.pay.vnet.Base64;
import com.ruoyi.project.pay.vnet.Base64Utils;

import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Map;

public class RSADemo {

    public static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
    public static final String ENCODE_ALGORITHM = "SHA-256";
    public static final String PLAIN_TEXT = "test string";

    public static void main(String[] args) {
        // 公私钥对
        try{
            Map<String, byte[]> keyMap = RSA.generateKeyBytes();
            String publicKeyStr = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDCKmydwIVCxwCu3ahVssQi2xsfdAdePV3hmrmk8SydhHUkZHN7vTPxZmsIFnJrBvKdKgFroCGwCi0JJHLrlgFAQsHGmd2/+RQmf8AVMpYVreVEF8k99qY7Qdp24pMlmljVujchjyXEQhuWfOhpfVFUW+Yjh45fyFqxUn3GVqfzLOxKkT/EVbnv2l7mCnQMnGJGPagaqjILFsZEu1Gj/8K/gFmtrX1yi0WUjaQeGKoCu0BMkFR8e5pe7Hmg4PfAwLnUNHmA02YB147oNWHORIBYwBZ9LcJJDpLwXmNWck6dYMq53VZC/Mg3gFjnhhPQVa5tuqSxZJSGCVpbSnEXcd/5AgMBAAECggEAKRMgdx7LEh1XH6b0fWyb4WPIkRbSmDUTLy60Jxh9/7SNs20soPidjGvGKKfW/J0zpdvFASeW+M6uSEjVptDM4vM8Wu8h4gKnKJ3N3K4e4ecvoQAHKnyLHe4IcS3tQv8r28rQ7uCBdMsnWuaBpA9GrTnjrZB3pfuUc/6nbNXYIbD4xsfjnlniNHUYBBsKdxvqGH53s/wzjaGPQJLwE+wJApElQr9IRyk6t/M9Sxypv/JyzP8+IspFs2Gzd61akJRAwZq9DVl7sej5Pj2xNFWnV6w8GVzIz+5d/1W4eO5ijq86MBxZlFWpn1z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8lxEIblnzoaX1RVFvmI4eOX8hasVJ9xlan8yzsSpE/xFW579pe5gp0DJxiRj2oGqoyCxbGRLtRo//Cv4BZra19cotFlI2kHhiqArtATJBUfHuaXux5oOD3wMC51DR5gNNmAdeO6DVhzkSAWMAWfS3CSQ6S8F5jVnJOnWDKud1WQvzIN4BY54YT0FWubbqksWSUhglaW0pxF3Hf+QIDAQAB";
            PublicKey publicKey = RSA.restorePublicKey(Base64.decode(publicKeyStr));
//            PrivateKey privateKey = RSA.restorePrivateKey(privateKeyStr.getBytes());
//            // 签名
//            byte[] sing_byte = sign(privateKey, PLAIN_TEXT);
//            // 验签
//            verifySign(publicKey, PLAIN_TEXT, sing_byte);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 签名
     *
     * @param privateKey 私钥
     * @param plain_text 明文
     * @return
     */
    public static byte[] sign(PrivateKey privateKey, String plain_text) {
        MessageDigest messageDigest;
        byte[] signed = null;
        try {
            messageDigest = MessageDigest.getInstance(ENCODE_ALGORITHM);
            messageDigest.update(plain_text.getBytes());
            byte[] outputDigest_sign = messageDigest.digest();
            System.out.println("SHA-256加密后-----》" + bytesToHexString(outputDigest_sign));
            Signature Sign = Signature.getInstance(SIGNATURE_ALGORITHM);
            Sign.initSign(privateKey);
            Sign.update(outputDigest_sign);
            signed = Sign.sign();
            System.out.println("SHA256withRSA签名后-----》" + bytesToHexString(signed));
        } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
            e.printStackTrace();
        }
        return signed;
    }

    /**
     * 验签
     *
     * @param publicKey  公钥
     * @param plain_text 明文
     * @param signed     签名
     */
    public static boolean verifySign(PublicKey publicKey, String plain_text, byte[] signed) {

        MessageDigest messageDigest;
        boolean SignedSuccess = false;
        try {
            messageDigest = MessageDigest.getInstance(ENCODE_ALGORITHM);
            messageDigest.update(plain_text.getBytes());
            byte[] outputDigest_verify = messageDigest.digest();
            //System.out.println("SHA-256加密后-----》" +bytesToHexString(outputDigest_verify));
            Signature verifySign = Signature.getInstance(SIGNATURE_ALGORITHM);
            verifySign.initVerify(publicKey);
            verifySign.update(outputDigest_verify);
            SignedSuccess = verifySign.verify(signed);
            System.out.println("验证成功？---" + SignedSuccess);

        } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException e) {
            e.printStackTrace();
        }
        return SignedSuccess;
    }

    /**
     * bytes[]换成16进制字符串
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
}